package me.samlss.lighter.util;


import me.samlss.lighter.parameter.LighterParameter;
import me.samlss.lighter.view.LighterView;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.utils.RectFloat;

/**
 * @author: zhu_zhonglin
 * @time: 2021/4/25
 */
public class ViewUtils {
    /**
     * 默认高亮显示背景颜色
     */
    public static final int DEFAULT_HIGHLIGHT_VIEW_BG_COLOR = 0x55000000;

    private ViewUtils(){
        throw new UnsupportedOperationException("Can not be instantiated.");
    }

    /**
     * isAttachedToWindow
     * @param view view
     * @return 是否附在window上
     */
    public static boolean isAttachedToWindow(Component view) {
        if (view == null) {
            LogUtil.loge("=== step1");
            return false;
        }
        /*if (Build.VERSION.SDK_INT >= 19) {
            return view.isAttachedToWindow();
        } else {
            return view.getWindowToken() != null;
        }*/
        boolean boundToWindow = view.isBoundToWindow();
        boolean tag = view.getTag() == null;
        LogUtil.loge("===boundToWindow:" + boundToWindow + " tag:" + tag);
        return boundToWindow || tag;
    }

    /**
     * Get the position of the view relative to the screen.
     * @param view The view.
     * @return RectFloat
     */
    public static RectFloat getRectOnScreen(Component view)
    {
        if (view == null){
            LogUtil.loge("ViewUtils", "Please pass non-null referParent and child.");
            return new RectFloat();
        }

        RectFloat result = new RectFloat();

        int[] pos = view.getLocationOnScreen();

        result.left   = pos[0];
        result.top    = pos[1];
        result.right  = result.left + view.getWidth();
        result.bottom = result.top  + view.getHeight();

        return result;
    }

    /**
     * Calculate the rect of the highlighted view
     *
     * @param lighterView
     * @param lighterParameter
     */
    public static void calculateHighlightedViewRect(LighterView lighterView, LighterParameter lighterParameter){
        if (lighterView == null
                || lighterParameter == null
                || lighterParameter.getHighlightedView() == null){
            return;
        }

        RectFloat highlightedViewRect = ViewUtils.getRectOnScreen(lighterParameter.getHighlightedView());

        if (highlightedViewRect == null || highlightedViewRect.isEmpty()){
            return;
        }

        LogUtil.loge("!!!" + "left:" + highlightedViewRect.left
                + "top:" + highlightedViewRect.top
                + "right:" + highlightedViewRect.right
                + "bottom:" + highlightedViewRect.bottom
        );

        ComponentContainer parent = (ComponentContainer) lighterView.getComponentParent();
        int[] rootViewPos = parent.getLocationOnScreen();

        highlightedViewRect.left = (float) ((double)highlightedViewRect.left - rootViewPos[0]);
        highlightedViewRect.right = (float) ((double)highlightedViewRect.right - rootViewPos[0]);
        highlightedViewRect.top = (float) ((double)highlightedViewRect.top - rootViewPos[1]);
        highlightedViewRect.bottom = (float) ((double)highlightedViewRect.bottom - rootViewPos[1]);

        highlightedViewRect.left = (float) ((double)highlightedViewRect.left - parent.getPaddingLeft());
        highlightedViewRect.right = (float) ((double)highlightedViewRect.right - parent.getPaddingLeft());
        highlightedViewRect.top = (float) ((double)highlightedViewRect.top - parent.getPaddingTop());
        highlightedViewRect.bottom = (float) ((double)highlightedViewRect.bottom - parent.getPaddingTop());

        lighterParameter.setHighlightedViewRect(highlightedViewRect);
        lighterParameter.getLighterShape().setViewRect(new RectFloat(
                (float) ((double)highlightedViewRect.left - (double) lighterParameter.getShapeXOffset()),
                (float) ((double)highlightedViewRect.top - (double)lighterParameter.getShapeYOffset()),
                (float) ((double)highlightedViewRect.right + (double)lighterParameter.getShapeXOffset()),
                (float) ((double)highlightedViewRect.bottom + (double)lighterParameter.getShapeYOffset())

        ));
    }
}
